package com.jt.tenant.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.GSPaymentActivity;
import com.jt.tenant.activity.OilQrcodeActivity;
import com.jt.tenant.adapter.MyOrderRefuelAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.CancelOrder;
import com.jt.tenant.conn.GetMyOrder4;
import com.jt.tenant.dialog.MyTimePopup;
import com.jt.tenant.widget.MyListView;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 马长鸣 on 2016/8/20.
 */
public class OrderRefuelFragment extends AppV4Fragment implements View.OnClickListener,MyOrderRefuelAdapter.OnCancelListener{

    public static OrderRefuelRefreshListener orderRefuelRefreshListener;

    private MyListView order_listview;

    private TextView my_order_commit;

    private List<GetMyOrder4.OrderList> mInfo = new ArrayList<>();

    private MyOrderRefuelAdapter adapter;

    private GetMyOrder4 getMyOrder4;

    private String id,mid;

    private Intent intent;

    private CancelOrder cancelOrder;

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = JTApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_myorder_refuel, null));

        id = getActivity().getIntent().getStringExtra("id");

        init_view(view);

        adapter = new MyOrderRefuelAdapter(getActivity(), mInfo,this);

        order_listview.setAdapter(adapter);

        orderRefuelRefreshListener = new OrderRefuelRefreshListener() {
            @Override
            public void OrderRefuelRefresh() {

                mInfo.clear();

                getDate();

            }
        };

        order_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String state = mInfo.get(position).GASSTATE;
                mid = mInfo.get(position).GASCODE;
                switch (state){
                    case "0":

                        intent = new Intent(getActivity(), GSPaymentActivity.class);
                        intent.putExtra("order_id",mInfo.get(position).GASCODE );
                        intent.putExtra("total_price",mInfo.get(position).TOTAL );
                        intent.putExtra("gs_name",mInfo.get(position).GAS_NAME );
                        intent.putExtra("gs_address",mInfo.get(position).GAS_ADDRESS );
                        intent.putExtra("lon",mInfo.get(position).LON );
                        intent.putExtra("lat",mInfo.get(position).LAT );

                        startActivity(intent);


                        break;

                    case "1":

                        break;

                    case "2":

                        break;

                    case "3":

                        intent = new Intent(getActivity(), OilQrcodeActivity.class);
                        intent.putExtra("order_id",mInfo.get(position).GASCODE );
                        intent.putExtra("gs_name",mInfo.get(position).GAS_NAME );
                        intent.putExtra("gs_address",mInfo.get(position).GAS_ADDRESS );
                        intent.putExtra("lon",mInfo.get(position).LON );
                        intent.putExtra("lat",mInfo.get(position).LAT );

                        startActivity(intent);


                        break;

                    default:

                        break;
                }
            }
        });

        getMyOrder4 = new GetMyOrder4(new AsyCallBack<GetMyOrder4.Info>() {

            @Override
            public void onSuccess(String toast, int type, GetMyOrder4.Info info) throws Exception {

                mInfo.clear();

                mInfo.addAll(info.orderLists);

                adapter.notifyDataSetChanged();

            }

        });

        getDate();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
//        getDate();
    }

    private void init_view(View view) {

        order_listview = (MyListView) view.findViewById(R.id.order_listview4);

    }

    @Override
    public void onClick(View v) {


    }

    private void getDate() {

        getMyOrder4.date = OrderFragment.getTime();

        getMyOrder4.execute(getContext());

    }

    @Override
    public void onCancel(String orderId) {

        cancelOrder = new CancelOrder(new AsyCallBack<CancelOrder.Info>(){
            @Override
            public void onSuccess(String toast, int type, CancelOrder.Info info) throws Exception {
                //super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    UtilToast.show(getActivity(), "订单已取消");

                    getDate();

                }
            }
        });
        cancelOrder.code = orderId;
        cancelOrder.execute(getContext());
    }


    public abstract class OrderRefuelRefreshListener {

        public void OrderRefuelRefresh() {

            getMyOrder4.date = OrderFragment.getTime();

            getMyOrder4.execute(getContext());

        }

    }
}
